package com.cskaoyan.gateway.controller.order;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@RestController
public class OrderController {

    @Reference
    OrderCoreService orderCoreService;
    @Reference
    OrderQueryService orderQueryService;


    @PostMapping("shopping/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest){
        //获得userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        int index = userInfo.indexOf(",");
        String userIdStr = userInfo.substring(index + 1);
        long userId = Long.parseLong(userIdStr);
        request.setUserId(userId);

        String orderUniqueKey = UUID.randomUUID().toString();
        request.setUniqueKey(orderUniqueKey);

        //执行pipeline创建订单
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(request);
        //异常处理
        if(createOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            ResponseData responseData = new ResponseData();
            responseData.setResult(createOrderResponse);
            responseData.setTimestamp(System.currentTimeMillis());
            responseData.setSuccess(true);
            responseData.setMessage("success");
            responseData.setCode(200);
            return responseData;
        }
        return new ResponseUtil().setErrorMsg(createOrderResponse.getMsg());


    }

    @GetMapping("shopping/order")
    public ResponseData selectOrder(OrderListRequest orderListRequest, HttpServletRequest servletRequest){
        //获得userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        int index = userInfo.indexOf(",");
        String userIdStr = userInfo.substring(index + 1);
        long userId = Long.parseLong(userIdStr);

        //查询userId的订单信息
        orderListRequest.setUserId(userId);
        OrderListResponse orderListResponse = orderQueryService.queryAllOrderList(orderListRequest);
        ResponseData responseData = new ResponseData();
        responseData.setResult(orderListResponse);
        responseData.setTimestamp(System.currentTimeMillis());
        responseData.setSuccess(true);
        responseData.setMessage("success");
        responseData.setCode(200);

        return responseData;

    }

    @GetMapping("/shopping/order/{id}")
    public ResponseData selectOrderDetail(@PathVariable("id") String id, HttpServletRequest servletRequest){
        //获得userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        int index = userInfo.indexOf(",");
        String userIdStr = userInfo.substring(index + 1);
        long userId = Long.parseLong(userIdStr);

        //封装请求数据
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id);

        OrderDetailResponsee orderDetailResponsee = orderQueryService.queryOrderDetail(orderDetailRequest);
        orderDetailResponsee.setUserId(userId);

        ResponseData responseData = new ResponseData();
        responseData.setResult(orderDetailResponsee);
        responseData.setTimestamp(System.currentTimeMillis());
        responseData.setSuccess(true);
        responseData.setMessage("success");
        responseData.setCode(200);

        return responseData;
    }

    /**
     * 取消定单
     * @param map
     * @return
     */
    @PostMapping("/shopping/cancelOrder")
    public ResponseData CancelOrder(@RequestBody Map<String,String> map){
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(map.get("id"));
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        ResponseData responseData = new ResponseUtil().setData(cancelOrderResponse.getMsg());
        return  responseData;

    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("shopping/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(orderId);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        if (deleteOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(deleteOrderResponse.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteOrderResponse.getMsg());
    }

}
